#include "mainintro.h"

extern u8 p_png[];
extern u8 o_png[];
extern u8 r_png[];
extern u8 t_png[];
extern u8 a_png[];
extern u8 l_png[];
extern u8 ii_png[];
extern u8 introPortal_png[];

MainIntro::MainIntro(SoundManager* snd)
{
	_sndPtr = snd;
	
	Image* pImg = new Image();
	Image* oImg = new Image();
	Image* rImg = new Image();
	Image* tImg = new Image();
	Image* aImg = new Image();
	Image* lImg = new Image();
	Image* iiImg = new Image();
	Image* introPortalImg = new Image();
	
	if(pImg->LoadImage(p_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(oImg->LoadImage(o_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(rImg->LoadImage(r_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(tImg->LoadImage(t_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(aImg->LoadImage(a_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(lImg->LoadImage(l_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(iiImg->LoadImage(ii_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	if(introPortalImg->LoadImage(introPortal_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	
	pSprite.SetImage(pImg);
	pSprite.SetPosition(100, -100);
	
	oSprite.SetImage(oImg);
	oSprite.SetPosition(190, -100);
	
	rSprite.SetImage(rImg);
	rSprite.SetPosition(280, -100);
	
	tSprite.SetImage(tImg);
	tSprite.SetPosition(370, -100);
	
	aSprite.SetImage(aImg);
	aSprite.SetPosition(460, -100);
	
	lSprite.SetImage(lImg);
	lSprite.SetPosition(550, -100);
	
	iiSprite.SetImage(iiImg);
	iiSprite.SetPosition(470, 50);
	
	portalSprite.SetImage(introPortalImg);
	portalSprite.SetPosition(185, 50);
}

MainIntro::~MainIntro()
{

}

void MainIntro::Play(GameWindow* gwd)
{
	int height = 60;

	_sndPtr->PlaySound(Sound::PORTAL_GUN_FX);
	//Fly in P
	for(int i = -100; i < height; i+=10)
	{
		pSprite.SetPosition(pSprite.GetX(), i);
		pSprite.Draw();
		gwd->Flush();
	}

	_sndPtr->PlaySound(Sound::PORTAL_GUN_FX);
	//Fly in o
	for(int i = -100; i < height; i+=10)
	{
		oSprite.SetPosition(oSprite.GetX(), i);
		pSprite.Draw();
		oSprite.Draw();
		gwd->Flush();
	}

	_sndPtr->PlaySound(Sound::PORTAL_GUN_FX);
	//Fly in r
	for(int i = -100; i < height; i+=10)
	{
		rSprite.SetPosition(rSprite.GetX(), i);
		pSprite.Draw();
		oSprite.Draw();
		rSprite.Draw();
		gwd->Flush();
	}
	
	_sndPtr->PlaySound(Sound::PORTAL_GUN_FX);
	//Fly in t
	for(int i = -100; i < height; i+=10)
	{
		tSprite.SetPosition(tSprite.GetX(), i);
		pSprite.Draw();
		oSprite.Draw();
		rSprite.Draw();
		tSprite.Draw();
		gwd->Flush();
	}
	
	_sndPtr->PlaySound(Sound::PORTAL_GUN_FX);
	//Fly in a
	for(int i = -100; i < height; i+=10)
	{
		aSprite.SetPosition(aSprite.GetX(), i);
		pSprite.Draw();
		oSprite.Draw();
		rSprite.Draw();
		tSprite.Draw();
		aSprite.Draw();
		gwd->Flush();
	}
	
	_sndPtr->PlaySound(Sound::PORTAL_GUN_FX);
	//Fly in l
	for(int i = -100; i < height; i+=10)
	{
		lSprite.SetPosition(lSprite.GetX(), i);
		pSprite.Draw();
		oSprite.Draw();
		rSprite.Draw();
		tSprite.Draw();
		aSprite.Draw();
		lSprite.Draw();
		gwd->Flush();
	}
	
	_sndPtr->PlaySound(Sound::PORTAL_GUN_FX);
	//Fire Portal
	for(int i = 0; i < 255; i+=50)
	{
		portalSprite.SetTransparency(i);
		pSprite.Draw();
		portalSprite.Draw();
		rSprite.Draw();
		tSprite.Draw();
		aSprite.Draw();
		lSprite.Draw();
		gwd->Flush();
	}
	
	_sndPtr->PlaySound(Sound::PORTAL_ENTER_FX);
	int x_pos = 650;
	//slide in i and fade out l
	for(int i = 255; i > 0; i-=10)
	{
		if(x_pos > 310)
		{
			x_pos--;
		}
		pSprite.Draw();
		portalSprite.Draw();
		rSprite.Draw();
		tSprite.Draw();
		aSprite.SetTransparency(i);
		aSprite.Draw();
		lSprite.SetTransparency(i);
		lSprite.Draw();
		
		iiSprite.SetPosition(x_pos, iiSprite.GetY());
		iiSprite.Draw();
		
		gwd->Flush();
	}
	
	Quad fadeQuad;
	GXColor fill_color;
	fill_color.r = 255;
	fill_color.g = 255;
	fill_color.b = 255;
	fill_color.a = 0;
	fadeQuad.SetFillColor(fill_color);
	fadeQuad.SetWidth(700);
	fadeQuad.SetHeight(700);
	fadeQuad.SetPosition(-30,-30);

	//Flash to white
	for(int i = 0; i < 255; i+=15)
	{
		pSprite.Draw();
		portalSprite.Draw();
		rSprite.Draw();
		tSprite.Draw();
		iiSprite.Draw();
		
		fill_color.a = i;
		fadeQuad.SetFillColor(fill_color);
		fadeQuad.Draw();
		
		gwd->Flush();
	}
	
	fill_color.a = 255;
	fadeQuad.SetFillColor(fill_color);
		
	for(int i = 0; i < 20; i++)
	{
		fadeQuad.Draw();
			
		gwd->Flush();
	}
}
